#include "../../head/utils/SerialUtil.h"
#include <QtDebug>

#include <QThread>
#include<QtSerialPort/QSerialPort>      //串口功能头文件
#include<QByteArray>
#include<QTimer>
#include<QString>
#include<QMessageBox>
#include<QCoreApplication>
#include <QTime>
#include "../../head/utils/DemoduleUtil.h"


//QList<double> list1;
//QList<double> list;
extern DemoduleUtil *demoduleUtil;
SerialUtil::SerialUtil(QObject *parent):QObject(parent)
{
    //设置串口属性
    serialPort = new QSerialPort(this);
    serialPort->setReadBufferSize(SERIAL_BUF_COUNT_SIZE);


    fifoReceiver1.Buf = (double*)malloc(SERIAL_BUF_COUNT_SIZE*sizeof(double));
    memset(fifoReceiver1.Buf,0,SERIAL_BUF_COUNT_SIZE*sizeof(double));
    fifoReceiver1.nWp = 0;
    fifoReceiver1.nRp = 0;
    fifoReceiver1.nUsedCnt = 0;
    fifoReceiver1.deep = SERIAL_BUF_COUNT_SIZE;

    QTimer *timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this, [=](){
        // qDebug() << "timeout \n" << endl;
        /************************************************
         * 使用长度超过接收BUFFER的总长度，串口出现问题点
         * 1、接收的数据格式不正确；
         * 2、串口自身出现问题；需要重置；
         * 3、图形渲染与串口阻塞；导致渲染时间过长；
         * *********************************************/
        if(fifoReceiver1.nUsedCnt > fifoReceiver1.deep){
            this->resetParam();

        }
        if(serialPort->isOpen()){
            QByteArray array = serialPort->readAll();
            //QByteArray array = serialPort->read(SERIAL_BUF_COUNT_SIZE);
            int readnb = array.size();

            if(readnb > 0){
                for (int i = 0; i < readnb; i++){
                    fifoReceiver1.Buf[fifoReceiver1.nWp] = (uint8_t)array[i];
                    fifoReceiver1.nUsedCnt++;
                    if(fifoReceiver1.nWp < SERIAL_BUF_COUNT_SIZE-1){
                        fifoReceiver1.nWp++;
                    }else{
                        fifoReceiver1.nWp=0;
                    }
                }
            }

            if(find_start_lable == 0){
                for(int i = 0 ; i < 4 ; i ++){
                    if ((fifoReceiver1.Buf[i] == 255) && (fifoReceiver1.Buf[i+1]== 255)){
                        find_start_lable=1;
                        fifoReceiver1.nRp=i+2;
                        fifoReceiver1.nUsedCnt=fifoReceiver1.nUsedCnt-2;
                        qDebug() << "SCICOM ok, \n" << endl;
                        break;
                    }else{
                        qDebug() << "SCICOM error3, \n" << endl;
                    }

                }
            }

            if(find_start_lable == 1 && fifoReceiver1.nUsedCnt >= FRAGMENT_BUF_SIZE){
                emit sendToDemoudle();           
            }
        }
    });
    timer->start(400);

    //initParam();
    //         connect(serialPort,&QSerialPort::readyRead,
    //                 [=]()
    //         {

    //            QByteArray array = serialPort->readAll();
    //            int readnb = array.size();


    //            if(readnb > 0){
    //                for (int i = 0; i < readnb; i++){
    //                    // list1.append((uint8_t)array[i]);
    //                    fifoReceiver1.Buf[fifoReceiver1.nWp] = (uint8_t)array[i];
    //                    fifoReceiver1.nUsedCnt++;
    //                    if(fifoReceiver1.nWp < SERIAL_BUF_COUNT_SIZE-1){
    //                        fifoReceiver1.nWp++;
    //                    }else{
    //                        fifoReceiver1.nWp=0;
    //                    }
    //                }
    //            }

    //            if(find_start_lable == 0){
    //                for(int i = 0 ; i < 4 ; i ++){
    //                    if ((fifoReceiver1.Buf[i] == 255) && (fifoReceiver1.Buf[i+1]== 255)){
    //                        find_start_lable=1;
    //                        fifoReceiver1.nRp=i+2;
    //                        fifoReceiver1.nUsedCnt=fifoReceiver1.nUsedCnt-2;
    //                        qDebug() << "SCICOM ok, \n" << endl;
    //                        break;
    //                    }else{
    //                        qDebug() << "SCICOM error3, \n" << endl;
    //                    }

    //                }
    //            }

    //            if(find_start_lable == 1 && fifoReceiver1.nUsedCnt >= FRAGMENT_BUF_SIZE){
    //                if(fifoReceiver1.Buf[fifoReceiver1.nRp] == 255
    //                        && fifoReceiver1.Buf[fifoReceiver1.nRp+1]== 255){
    //                  qDebug() << "nRp nRp+1 == 255 \n" << endl;
    //                  this->resetParam();
    //                }else{
    //                    //qDebug()<< QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    //                    emit sendToDemoudle123();
    //                }
    //            }

    //        });
}



QString SerialUtil::connectSerail(){
    //触发信号
    if(serialPort->open(QIODevice::ReadOnly) == true)
    {
        //qDebug() << "=======串口线程连接成功============" << endl;
        serial_Connect_State = true;
        return "true";

    }else{
        // qDebug() << "=======串口线程连接失败============" << endl;
        serial_Connect_State = false;
        return "false";
    }

}


void SerialUtil::resetParam(){
    memset(fifoReceiver1.Buf,0,SERIAL_BUF_COUNT_SIZE*sizeof(double));
    fifoReceiver1.nWp = 0;
    fifoReceiver1.nRp = 0;
    fifoReceiver1.nUsedCnt = 0;
    fifoReceiver1.deep = SERIAL_BUF_COUNT_SIZE;
    find_start_lable = 0;
    demoduleUtil->record_log("SCICOM reset..." );
    //qDebug() << "SCICOM reset..." << endl;
    serialPort->close();
    serialPort->open(QIODevice::ReadOnly);
    //qDebug() << "SCICOM reset ok..." << endl;
    demoduleUtil->record_log("SCICOM reset ok..." );
}
